﻿using Reader.Api;
using Reader.Api.Alipay;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/5/28 0:26:39
* */
namespace Reader.Web.api
{
    public partial class alipay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SortedDictionary<string, string> sPara = GetRequestPost();

            if (sPara.Count > 0)//判断是否有带返回参数
            {
                Notify aliNotify = new Notify();
                bool verifyResult = aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"]);

                if (verifyResult)//验证成功
                {
                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    //请在这里加上商户的业务逻辑程序代码

                    int UID = 0;


                    float Fee = 0f;
                    try { Fee = float.Parse(Request.Form["total_fee"]); }
                    catch { Response.Write("fail"); return; }

                    string buyer_email = Request.Form["buyer_email"];




                    //——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
                    //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表

                    //商户订单号

                    string out_trade_no = Request.Form["out_trade_no"];
                    try { UID = int.Parse(out_trade_no.Substring(out_trade_no.Length - 5, 5)); }
                    catch { Response.Write("fail"); return; }
                    //支付宝交易号

                    string trade_no = Request.Form["trade_no"];

                    //交易状态
                    string trade_status = Request.Form["trade_status"];
                    if (Request.Form["trade_status"] == "TRADE_FINISHED")
                    {
                        if (!DB.isOrdID(UID, out_trade_no))
                        {
                            if (DB.Recharge(UID, GetMoney(Fee), "android","用户ID(" + UID + ")充值" + Fee + "元,支付宝账号:" + buyer_email, out_trade_no))
                            {
                                Response.Write("success");  //请不要修改或删除
                            }
                            else
                            {
                                Response.Write("fail");
                            }
                        }
                    }
                    else if (Request.Form["trade_status"] == "TRADE_SUCCESS")
                    {
                        if (!DB.isOrdID(UID, out_trade_no))
                        {
                            if (DB.Recharge(UID, GetMoney(Fee), "android", "用户ID(" + UID + ")充值" + Fee + "元,支付宝账号:" + buyer_email, out_trade_no))
                            {
                                Response.Write("success");  //请不要修改或删除
                            }
                            else
                            {
                                Response.Write("fail");
                            }
                        }
                    }
                    else
                    {
                        Response.Write("fail");
                    }

                    //——请根据您的业务逻辑来编写程序（以上代码仅作参考）——



                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
                }
                else//验证失败
                {
                    Response.Write("fail");
                }
            }
            else
            {
                Response.Write("无通知参数");
            }

        }


        /// <summary>
        /// 获取支付宝POST过来通知消息，并以“参数名=参数值”的形式组成数组
        /// </summary>
        /// <returns>request回来的信息组成的数组</returns>
        public SortedDictionary<string, string> GetRequestPost()
        {
            int i = 0;
            SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
            NameValueCollection coll;
            coll = Request.Form;//怎么接收不到数据啊
            String[] requestItem = coll.AllKeys;
            string str = "";
            for (i = 0; i < requestItem.Length; i++)
            {
                str += requestItem[i] + "=" + Request.Form[requestItem[i]] + "&";
                sArray.Add(requestItem[i], Request.Form[requestItem[i]]);
            }
           // Core.LogResult(str, "Log_" +  new Random().Next(1000));
            return sArray;
        }



        private float GetMoney(float Fee)
        {
            if (Fee == 5)
            {
                return 500f;
            }
            else if (Fee == 10)
            {
                return 1500f;
            }
            else if (Fee == 20)
            {
                return 2800f;
            }
            else if (Fee == 30)
            {
                return 5000f;
            }
            else if (Fee == 50)
            {
                return 9000f;
            }
            else if (Fee == 100)
            {
                return 20000f;
            }
            else
            {
                return (float)Fee * 100;
            }
        }




    }
}